package org.csu.jpetstore.web.servlet;

import org.csu.jpetstore.domain.Account;
import org.csu.jpetstore.service.AccountService;
import org.csu.jpetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

public class EditAccountServlet extends HttpServlet {

    private static final String EDIT_ACCOUNT_FORM = "/WEB-INF/jsp/account/EditAccountForm.jsp";

    private Account account;
    private String confirmPwd;
    private String msg;
    private LogService logService;

    private void getParameters(HttpServletRequest request) {

        this.account = new Account();
        account.setUsername(request.getParameter("username"));
        account.setPassword(request.getParameter("password"));
        this.confirmPwd = request.getParameter("repeatedPassword");
        account.setFirstName(request.getParameter("firstName"));
        account.setLastName(request.getParameter("lastName"));
        account.setEmail(request.getParameter("email"));
        account.setPhone(request.getParameter("phone"));
        account.setAddress1(request.getParameter("address1"));
        account.setAddress2(request.getParameter("address2"));
        account.setCity(request.getParameter("city"));
        account.setState(request.getParameter("state"));
        account.setZip(request.getParameter("zip"));
        account.setCountry(request.getParameter("country"));
        account.setLanguagePreference(request.getParameter("languagePreference"));
        account.setFavouriteCategoryId(request.getParameter("favouriteCategoryId"));
        account.setBannerName(request.getParameter("favouriteCategoryId"));
        if(request.getParameter("listOption")!=null)
            account.setListOption(true);
        else
            account.setListOption(false);
        if(request.getParameter("bannerOption")!=null)
            account.setBannerOption(true);
        else
            account.setBannerOption(false);
        HttpSession session = request.getSession();
        session.setAttribute("loginAccount", account);
    }

    private boolean validation() {
        if(this.account.getPassword() == null || this.account.getPassword().trim().equals("") &&
           this.confirmPwd != null && !this.confirmPwd.trim().equals("")) {
            this.msg = "请填密码";
            return false;
        }
        if(this.confirmPwd == null || this.confirmPwd.trim().equals("") &&
           this.account.getPassword() != null && !this.account.getPassword().trim().equals("")) {
            this.msg = "请填确认密码";
            return false;
        }
        if(this.account.getPassword() != null && !this.account.getPassword().trim().equals("") &&
           this.confirmPwd != null && !this.confirmPwd.trim().equals("") &&
           !this.confirmPwd.equals(this.account.getPassword())) {
            this.msg = "两次密码不一致";
            return false;
        }
        if(this.account.getFirstName() == null || this.account.getFirstName().trim().equals("")) {
            this.msg = "名必填";
            return false;
        }
        if(this.account.getLastName() == null || this.account.getLastName().trim().equals("")) {
            this.msg = "姓必填";
            return false;
        }
        if(this.account.getEmail() == null || this.account.getEmail().trim().equals("")) {
            this.msg = "邮箱必填";
            return false;
        }
        if(this.account.getPhone() == null || this.account.getPhone().trim().equals("")) {
            this.msg = "电话号码必填";
            return false;
        }
        if(this.account.getAddress1() == null || this.account.getAddress1().trim().equals("")) {
            this.msg = "地址一必填";
            return false;
        }
        if(this.account.getAddress2() == null || this.account.getAddress2().trim().equals("")) {
            this.msg = "地址二必填";
            return false;
        }
        if(this.account.getCity() == null || this.account.getCity().trim().equals("")) {
            this.msg = "城市必填";
            return false;
        }
        if(this.account.getState() == null || this.account.getState().trim().equals("")) {
            this.msg = "州必填";
            return false;
        }
        if(this.account.getZip() == null || this.account.getZip().trim().equals("")) {
            this.msg = "邮政编码必填";
            return false;
        }
        if(this.account.getCountry() == null || this.account.getCountry().trim().equals("")) {
            this.msg = "国家必填";
            return false;
        }
        return true;
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logService = new LogService();

        request.setCharacterEncoding("UTF-8");
        getParameters(request);
        if (validation()) {
            AccountService accountService = new AccountService();
            int result = 0;
            if(this.account.getPassword() != null && !this.account.getPassword().trim().equals("") &&
                    this.confirmPwd != null && !this.confirmPwd.trim().equals("")) {
                accountService.updateAccount(this.account);
                accountService.updatePwd(this.account);
                if(accountService.updateAccount(this.account)==1 && accountService.updatePwd(this.account)==1)
                    result =1;
            }
            else{
                result = accountService.updateAccount(this.account);
            }
            if(result == 1) {
                this.msg= "更新成功";
                request.setAttribute("msg", this.msg);
            }
        }
        else {
            request.setAttribute("msg", this.msg);
        }
        HttpSession session = request.getSession();

        Account user = (Account) session.getAttribute("loginAccount");
        Date date = new Date();
        logService.insert(user.getUsername(), date.toString(), "编辑账号信息");

        request.getRequestDispatcher(EDIT_ACCOUNT_FORM).forward(request, response);
    }
}